package com.cheyun.netsalev3.widget

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.BaseDialogLayoutBinding
import com.cheyun.netsalev3.utils.DialogUtil
import com.tencent.imsdk.utils.IMFunc


/**
 * 基础弹窗.
 */
class BaseDialog : DialogFragment() {
    lateinit var binding: BaseDialogLayoutBinding
    lateinit var adapter: MyAdapter
    var listener: BaseDialogListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_FRAME,R.style.share_activity_styles)
    }

    /**系统调用这个来获得对话片段的布局，不管是否显示为对话框或嵌入片段。*/
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.e("onCreateView", "................")
        //将布局膨胀为对话框或嵌入片段
        binding = DataBindingUtil.inflate(inflater, R.layout.base_dialog_layout, container, false)
        listener?.apply {
            binding.viewModel = listener
        }

        initView()
        binding.setLifecycleOwner(this)
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        // 设置底部弹出显示的DialogFragment窗口属性。
        val window = dialog?.window!!
        var params = window.attributes
        params.gravity = Gravity.BOTTOM
        params.width = WindowManager.LayoutParams.MATCH_PARENT

        if (!IMFunc.isBrandVivo()) {
            DialogUtil.getDialogWindow(this!!.activity, window)
        }
        window.setBackgroundDrawableResource(android.R.color.transparent)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

        window.attributes = params
    }

    interface BaseDialogListener {


        /**
         * 标题
         */
        var title: String
        /**
         * 内容
         */
        var content: String?
        /**
         * 列表数据
         */
        var list: MutableLiveData<List<*>>
        /**
         * 列表item
         */
        var layoutId: Int

        /**
         * 点击取消
         */
        fun onClickCancel(view: View)

        /**
         * 点击确定
         */
        fun onClickOk(view: View)

        /**
         * 列表item点击
         */
        fun onClickItem(view: View, param: Any)

    }


    private fun initView() {
        binding.cancelButton.setOnClickListener {
            dismiss()
            listener?.onClickCancel(it)
        }
        binding.okButton.setOnClickListener {
            dismiss()
            listener?.onClickOk(it)
        }


        if (listener?.list != null && listener?.list is MutableLiveData<List<*>>) {
            adapter = MyAdapter(listener!!.layoutId, binding.viewModel)
            binding.recyclerView.adapter = adapter
            binding.viewModel?.let {
                it.list.observe(this, Observer { list ->
                    run {
                        binding.recyclerView.visibility = if (list.isEmpty()) View.GONE else View.VISIBLE
                        adapter.submitList(list)
                    }
                })
            }
        }
    }


    /**系统仅在对话框中创建布局时调用此操作。*/
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        Log.e("onCreateDialog", "................")
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        val window = dialog.window

        window.setBackgroundDrawableResource(android.R.color.transparent)
        return dialog
    }


}